<?php

namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use App\Admin\Models\PermissionRole;
use App\Admin\Models\Permission;

/**
 * @todo: Refactor this class by using something like MenuBuilder Helper.
 */
class Menu extends Model
{
    protected $table = 'admin_menus';

    protected $guarded = [];

    private static $permissions;
    private static $user_permissions;
    private static $dataTypes;
    private static $prefix;


    /**
     * Display menu.
     *
     * @param string      $menuName
     * @param string|null $type
     * @param array       $options
     *
     * @return string
     */
    public static function display($menuName, $type = null, $options = [])
    {
        // GET THE MENU
        $menuItems = MenuItem::get();

        // Check for Menu Existence
        if (!isset($menuItems)) {
            return false;
        }

        self::$prefix = trim(route('admin.dashboard', [], false), '/');

        return self::buildAdminMenuOutput($menuItems, '', $options, request());

    }

    /**
     * Create admin menu.
     *
     * @param \Illuminate\Support\Collection|array $menuItems
     * @param string                               $output
     * @param object                               $options
     * @param \Illuminate\Http\Request             $request
     *
     * @return string
     */
    public static function buildAdminMenuOutput($menuItems, $output, $options, Request $request, $child = null)
    {
        $role_id = Auth::user()->role_id;
        $permission = PermissionRole::getPermissionInfo($role_id);
        $permissions = Permission::getPermissionInfo($permission);
        if (!$child) {
            $parentItems = $menuItems->filter(function ($value, $key) {
                return $value->parent_id == null;
            });
        } else {
            $parentItems = $menuItems->filter(function ($value, $key) use ($child) {
                return $value->parent_id == $child;
            });
        }

        $parentItems = $parentItems->sortBy('order');

        foreach ($parentItems as $item) {
            $li_class = '';
            $collapse_id = '';

            if ($request->is(ltrim($item->url, '/'))) {
                $li_class = ' class="active"';
            }

            $children_menu_items = $menuItems->filter(function ($value, $key) use ($item) {
                return $value->parent_id == $item->id;
            });
            if ($children_menu_items->count() > 0) {
                if ($li_class != '') {
                    $li_class = rtrim($li_class, '"').' dropdown"';
                } else {
                    $li_class = ' class="dropdown"';
                }

                $collapse_id = $item->title.'-dropdown-element';
                $a_attrs = 'data-toggle="collapse" href="#'.$collapse_id.'"';
            } else {
                $a_attrs = 'href="'.$item->url.'"';
            }


            $children_output = null;

            if ($children_menu_items->count() > 0) {
                $children_output = self::buildAdminMenuOutput($menuItems, '', [], $request, $item->id);

                if ($children_output == '') {
                    continue;
                }
            }
            $menuBool = false;
            $userPermissions[] = "主页";
            foreach ($permissions as $table=>$permission) {
                $userPermissions[] = $table;
            }
            foreach ($permissions as $permission) {
                foreach ($permission as $p) {
                    $userPermissions[] = $p->sign;
                }
            }
            if (in_array($item->sign, $userPermissions)) {
                $menuBool = true;
            }
            if ($menuBool) {
                $output .= '<li'.$li_class.'><a '.$a_attrs.' target="'.$item->target.'">'.'<span class="icon '.$item->icon_class.'"></span>'.'<span class="title">'.$item->title.'</span></a>';
            }

            if (!is_null($children_output)) {
                // Add tag for collapse panel
                $output .= '<div id="'.$collapse_id.'" class="panel-collapse collapse"><div class="panel-body">';
                //$output = self::buildAdminMenuOutput($menuItems, $output, [], $request, $item->id);
                $output .= $children_output;
                $output .= '</div></div>';      // close tag of collapse panel
            }

            $output .= '</li>';
        }

        if (empty($output)) {
            return '';
        }

        return '<ul class="nav navbar-nav">'.$output.'</ul>';
    }

}
